package com.chartHandle.excelService.handle;

import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.chartHandle.bean.CellStyleWY;
import com.chartHandle.bean.PointXY;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;

import java.util.List;
import java.util.Map;

// 颜色的处理，每一行一个颜色
public class WeeklySupplyStrategyDataHandler implements CellWriteHandler {
    // 每个单元格的颜色。key是行号，value 是单元格颜色的list集合
    private Map<PointXY, CellStyleWY> maps;

    public WeeklySupplyStrategyDataHandler(Map<PointXY, CellStyleWY> maps, int rows) {
        this.maps = maps;
        this.rows = rows;
    }

    private int rows;

    public void setRows(int rows) {
        rows = rows;
    }

    public int getRows() {
        return rows;
    }

    public void setMaps(Map<PointXY, CellStyleWY> maps) {
        this.maps = maps;
    }

    public WeeklySupplyStrategyDataHandler() {
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        int x = cell.getColumnIndex();
        int y = cell.getRowIndex();

        CellStyleWY cellStyleWY = maps.get(new PointXY(x, y));
        if (cellStyleWY == null) {
            return;
        }
        CellStyle cellStyle = writeSheetHolder.getSheet().getWorkbook().createCellStyle();
        //这一行是很关键的，getRow里面很重要，到底是克隆哪一行。克隆原本的样式，对原本的样式进行修改。
        cellStyle.cloneStyleFrom(writeSheetHolder.getCachedSheet().getRow(getRows()).getCell(x).getCellStyle());
        cellStyle.setFillForegroundColor(cellStyleWY.getColorIndex());
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        writeSheetHolder.getCachedSheet().getRow(y).createCell(x);
        Cell colorCell = writeSheetHolder.getCachedSheet().getRow(y).getCell(x);
        colorCell.setCellValue(cell.getStringCellValue());
        colorCell.setCellStyle(cellStyle);
    }
}
